package com.glory.good_dark.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.good_dark.R;
import com.glory.good_dark.adapter.PPAKBinInfoAdapter;
import com.glory.good_dark.application.App;
import com.glory.good_dark.application.BaseActivity;
import com.glory.good_dark.entity.LotIDInfoEntity;
import com.glory.good_dark.entity.PPAKInfoEntity;
import com.glory.good_dark.entity.response.BaseEntity;
import com.glory.good_dark.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

import org.json.JSONObject;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PPAKWorkEndActivity extends BaseProjectActivity {

    @BindView(R.id.EQPID_Txt)
    TextView EQPIDTxt;
    @BindView(R.id.lot_Txt)
    TextView lotTxt;
    @BindView(R.id.lot_Ly)
    LinearLayout lotLy;
    @BindView(R.id.rvPPAKInfoList)
    RecyclerView rvPPAKInfoList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private PPAKBinInfoAdapter adapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_ppak_work_end;
    }

    @Override
    protected void initView() {
        EQPIDTxt.setText(App.EQPID);
        initRecyclerView(rvPPAKInfoList,this);
        adapter = new PPAKBinInfoAdapter(null);
        adapter.isFirstOnly(true);
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        rvPPAKInfoList.setAdapter(adapter);
    }

    @Override
    protected void initData() {
//        String ss = "{\n" +
//                "    \"data\": {\n" +
//                "        \"BODY\": {\n" +
//                "            \"EQP_ID\": \"JHH009\",\n" +
//                "            \"OPERATOR\": \"Admin\",\n" +
//                "            \"LOTID\": \"P14431407-0011\",\n" +
//                "            \"ViewTestBinInfo\": [\n" +
//                "                {\n" +
//                "                    \"BIN_PROMPT\": \"BIN1\",\n" +
//                "                    \"BIN_PROMPT_DESC\": \"Pass\",\n" +
//                "                    \"BIN_GROUP\": \"Pass\",\n" +
//                "                    \"Quantity\": \"0\"\n" +
//                "                },\n" +
//                "                {\n" +
//                "                    \"BIN_PROMPT\": \"BIN2\",\n" +
//                "                    \"BIN_PROMPT_DESC\": \"OS\",\n" +
//                "                    \"BIN_GROUP\": \"OS\",\n" +
//                "                    \"Quantity\": \"0\"\n" +
//                "                },\n" +
//                "                {\n" +
//                "                    \"BIN_PROMPT\": \"BIN3\",\n" +
//                "                    \"BIN_PROMPT_DESC\": \"TR\",\n" +
//                "                    \"BIN_GROUP\": \"TR\",\n" +
//                "                    \"Quantity\": \"0\"\n" +
//                "                },\n" +
//                "                {\n" +
//                "                    \"BIN_PROMPT\": \"BIN4\",\n" +
//                "                    \"BIN_PROMPT_DESC\": \"SS\",\n" +
//                "                    \"BIN_GROUP\": \"SS\",\n" +
//                "                    \"Quantity\": \"0\"\n" +
//                "                },\n" +
//                "                {\n" +
//                "                    \"BIN_PROMPT\": \"BIN5\",\n" +
//                "                    \"BIN_PROMPT_DESC\": \"VF\",\n" +
//                "                    \"BIN_GROUP\": \"VF\",\n" +
//                "                    \"Quantity\": \"0\"\n" +
//                "                },\n" +
//                "                {\n" +
//                "                    \"BIN_PROMPT\": \"BIN6\",\n" +
//                "                    \"BIN_PROMPT_DESC\": \"VB\",\n" +
//                "                    \"BIN_GROUP\": \"VB\",\n" +
//                "                    \"Quantity\": \"0\"\n" +
//                "                },\n" +
//                "                {\n" +
//                "                    \"BIN_PROMPT\": \"BIN7\",\n" +
//                "                    \"BIN_PROMPT_DESC\": \"IR\",\n" +
//                "                    \"BIN_GROUP\": \"IR\",\n" +
//                "                    \"Quantity\": \"0\"\n" +
//                "                },\n" +
//                "                {\n" +
//                "                    \"BIN_PROMPT\": \"BIN8\",\n" +
//                "                    \"BIN_PROMPT_DESC\": \"DIR\",\n" +
//                "                    \"BIN_GROUP\": \"DIR\",\n" +
//                "                    \"Quantity\": \"0\"\n" +
//                "                },\n" +
//                "                {\n" +
//                "                    \"BIN_PROMPT\": \"BIN9\",\n" +
//                "                    \"BIN_PROMPT_DESC\": \"VFM\",\n" +
//                "                    \"BIN_GROUP\": \"VFM\",\n" +
//                "                    \"Quantity\": \"0\"\n" +
//                "                },\n" +
//                "                {\n" +
//                "                    \"BIN_PROMPT\": \"BIN10\",\n" +
//                "                    \"BIN_PROMPT_DESC\": \"DVF\",\n" +
//                "                    \"BIN_GROUP\": \"DVF\",\n" +
//                "                    \"Quantity\": \"0\"\n" +
//                "                },\n" +
//                "                {\n" +
//                "                    \"BIN_PROMPT\": \"BIN11\",\n" +
//                "                    \"BIN_PROMPT_DESC\": \"RC\",\n" +
//                "                    \"BIN_GROUP\": \"RC\",\n" +
//                "                    \"Quantity\": \"0\"\n" +
//                "                },\n" +
//                "                {\n" +
//                "                    \"BIN_PROMPT\": \"BIN12\",\n" +
//                "                    \"BIN_PROMPT_DESC\": \"YMK\",\n" +
//                "                    \"BIN_GROUP\": \"YMK\",\n" +
//                "                    \"Quantity\": \"0\"\n" +
//                "                },\n" +
//                "                {\n" +
//                "                    \"BIN_PROMPT\": \"BIN13\",\n" +
//                "                    \"BIN_PROMPT_DESC\": \"NMK\",\n" +
//                "                    \"BIN_GROUP\": \"NMK\",\n" +
//                "                    \"Quantity\": \"0\"\n" +
//                "                }\n" +
//                "            ]\n" +
//                "        },\n" +
//                "        \"HEADER\": {\n" +
//                "            \"MACHINENAME\": \"LIO-AN00\",\n" +
//                "            \"MESSAGENAME\": \"PDAGetppakTrackOutInfo\",\n" +
//                "            \"TRANSACTIONID\": \"7d86a74a-6892-4f7c-8f36-5da566b9babf\",\n" +
//                "            \"EVENTTIME\": \"2020-10-16 14:53:33\"\n" +
//                "        },\n" +
//                "        \"RETURN\": {\n" +
//                "            \"RETURNCODE\": 0,\n" +
//                "            \"RETURNMESSAGE\": null\n" +
//                "        }\n" +
//                "    }\n" +
//                "}";
//        BaseEntity<PPAKInfoEntity> bean = (new Gson()).fromJson(ss, new TypeToken<BaseEntity<PPAKInfoEntity>>() {
//        }.getType());
//        if (bean.isSuccess(getApplicationContext(), getSupportFragmentManager())) {
//            PPAKInfoEntity PPAKInfoEntity = bean.getData().getBody();
//            lotTxt.setText(PPAKInfoEntity.getLOTID());
//            adapter.setNewData(PPAKInfoEntity.getViewTestBinInfo());
//        }
        FreeUI_EntityCallBack<BaseEntity<PPAKInfoEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<PPAKInfoEntity>>
                (new TypeToken<BaseEntity<PPAKInfoEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<PPAKInfoEntity>> response) {
                if (response.body().isSuccess(getApplicationContext(), getSupportFragmentManager())) {
                    lotTxt.setText(response.body().getData().getBody().getLOTID());
                    adapter.setNewData(response.body().getData().getBody().getViewTestBinInfo());
                }
            }

            @Override
            public void onError
                    (Response<BaseEntity<PPAKInfoEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "PDAGetppakTrackOutInfo");
            }
        };

        JsonObject  map = new JsonObject();
        map.addProperty("EQP_ID",App.EQPID);
        map.addProperty("OPERATOR",App.userEntity.getWORKNO());
        NetUtils.requestPostNet(App.WEB_URL + "/api/PDA",this,callBack,"PDAGetppakTrackOutInfo",map);
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("PPAK批次结束作业");
        freeUI_titleView.setRightListener("结束作业", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FreeUI_EntityCallBack<BaseEntity<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<JSONObject>>
                        (new TypeToken<BaseEntity<JSONObject>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess
                            (final Response<BaseEntity<JSONObject>> response) {
                        if (response.body().isSuccess(getApplicationContext(), getSupportFragmentManager())) {
                            showConfirmDialog(PPAKWorkEndActivity.this,"结束作业成功");
                        }
                    }

                    @Override
                    public void onError
                            (Response<BaseEntity<JSONObject>> response) {
                        super.onError(response);
                        loadError(response.getException(), "PDAGetppakTrackOutSubmit");
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("EQP_ID",App.EQPID);
                map.addProperty("OPERATOR",App.userEntity.getWORKNO());
                map.addProperty("LOTID",lotTxt.getText().toString());
                map.add("BinList",new JsonParser().parse(new Gson().toJson(adapter.getData())).getAsJsonArray());
                NetUtils.requestPostNet(App.WEB_URL + "/api/PDA",PPAKWorkEndActivity.this,callBack,"PDAGetppakTrackOutSubmit",map);
            }
        });
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

    @Override
    protected void EQPReplaceSuccess(int tag, String content) {

    }
}